﻿using GFluentCode.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GFluentCode.ORM.MySql
{
    /// <summary>
    /// MySql系统表操作
    /// </summary>
    public class MySqlORM:ORMBase
    {
        public MySqlORM(string conn) : base(conn, EnDbType.MySql) { }

        /// <summary>
        /// 查询系统表名
        /// </summary>
        public IList<GFluentTableNameEntity> QueryTableNames(string dbName)
        {
            return QueryBySql<GFluentTableNameEntity>($@"
select table_name from information_schema.tables where table_schema='{dbName}'
");
        }

        /// <summary>
        /// 获取表对象
        /// </summary>
        public GFluentTableEntity QueryTableEntity(string tableName,string dbName)
        {
            var cols = QueryBySql<GFluentColumnEntity>($@"
select Table_Name TableName,Column_Name ColumnName,Data_Type ColumnType,Column_Comment ColumnRemark
from information_schema.columns 
where table_name='{tableName}' and table_schema='{dbName}'
");
            return new GFluentTableEntity { TableName = tableName, Columns = cols };
        }

        /// <summary>
        /// 获取表对象
        /// </summary>
        public IList<GFluentTableEntity> QueryTableEntitys(string dbName,params string[] tableNames)
        {
            var r = new List<GFluentTableEntity>();

            foreach (var tn in tableNames)
                r.Add(QueryTableEntity(tn,dbName));

            return r;
        }
    }
}
